﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using COM.Excel;
using System.IO;
using System.Data.SqlClient;

namespace SDLITS.Common
{
    public class Methed
    {
        public static long Transfer(string p)
        {
            long egNoA = Convert.ToInt64(p);
            long egNoB = egNoA % (256 * 256) + (Convert.ToInt32(egNoA / (256 * 256)) % 256) * 100000;

            return egNoB;
        }
        public static string Condition(string i)
        {
            if (i == "1")
            {
                return "<font color='green'>正 常</font>";
            }
            if (i == "2")
            {
                return "<font color='blueviolet'>冻 结</font>";
            }
            return "<font color='red'>异 常</font>";
        }
        public static string Status(string i)
        {
            if (i == "1")
            {
                return "<font color='green'>激 活</font>";
            }
            if (i == "2")
            {
                return "<font color='blueviolet'>空 闲</font>";
            }
            return "<font color='red'>异 常</font>";
        }
        public static void GridViewToExcel(DataTable MyData, Hashtable nameList, string ReportTitle)
        {
            string filePath = HttpContext.Current.Server.MapPath("../") + "ReportFile\\";
            //利用excel对象
            var dte = new Methed();
            string filename = "";
            try
            {
                if (MyData.Rows.Count > 0)
                {
                    filename = dte.DataExcel(MyData, ReportTitle, filePath, nameList);
                }
            }
            catch (Exception e)
            {
                string eS = e.Message;
            }
            if (filename != "")
            {
                HttpContext.Current.Response.Redirect("../ReportFile/" + filename, true);
            }
        }
        /// <summary>
        /// 将DataTable的数据导出显示为报表(不使用Excel对象)
        /// </summary>
        /// <param name="dt">数据DataTable</param>
        /// <param name="strTitle">标题</param>
        /// <param name="FilePath">生成文件的路径</param>
        /// <param name="nameList"></param>
        /// <returns></returns>
        public string DataExcel(DataTable dt, string strTitle, string FilePath, Hashtable nameList)
        {
            var excel = new cExcelFile();
            ClearFile(FilePath);
            string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
            excel.CreateFile(FilePath + filename);
            excel.PrintGridLines = false;

            cExcelFile.MarginTypes mt1 = cExcelFile.MarginTypes.xlsTopMargin;
            cExcelFile.MarginTypes mt2 = cExcelFile.MarginTypes.xlsLeftMargin;
            cExcelFile.MarginTypes mt3 = cExcelFile.MarginTypes.xlsRightMargin;
            cExcelFile.MarginTypes mt4 = cExcelFile.MarginTypes.xlsBottomMargin;

            double height = 1.5;
            excel.SetMargin(ref mt1, ref height);
            excel.SetMargin(ref mt2, ref height);
            excel.SetMargin(ref mt3, ref height);
            excel.SetMargin(ref mt4, ref height);

            cExcelFile.FontFormatting ff = cExcelFile.FontFormatting.xlsNoFormat;
            string font = "宋体";
            short fontsize = 9;
            excel.SetFont(ref font, ref fontsize, ref ff);

            byte b1 = 1,
                 b2 = 12;
            short s3 = 12;
            excel.SetColumnWidth(ref b1, ref b2, ref s3);

            string header = "页眉";
            string footer = "页脚";
            excel.SetHeader(ref header);
            excel.SetFooter(ref footer);


            cExcelFile.ValueTypes vt = cExcelFile.ValueTypes.xlsText;
            cExcelFile.CellFont cf = cExcelFile.CellFont.xlsFont0;
            cExcelFile.CellAlignment ca = cExcelFile.CellAlignment.xlsCentreAlign;
            cExcelFile.CellHiddenLocked chl = cExcelFile.CellHiddenLocked.xlsNormal;

            // 报表标题
            int cellformat = 1;
            int rowIndex = 1; //起始行
            int colIndex = 0;


            //取得列标题				
            foreach (DataColumn colhead in dt.Columns)
            {
                colIndex++;
                string name = colhead.ColumnName.Trim();
                object namestr = name;
                IDictionaryEnumerator Enum = nameList.GetEnumerator();
                while (Enum.MoveNext())
                {
                    if (Enum.Key.ToString().Trim() == name)
                    {
                        namestr = Enum.Value;
                    }
                }
                excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref namestr,
                                 ref cellformat);
            }

            //取得表格中的数据			
            foreach (DataRow row in dt.Rows)
            {
                rowIndex++;
                colIndex = 0;
                foreach (DataColumn col in dt.Columns)
                {
                    colIndex++;
                    if (col.DataType == Type.GetType("System.DateTime"))
                    {
                        if (!(row[col.ColumnName] is DBNull))
                        {
                            object str = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                            excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str,
                                             ref cellformat);
                        }
                    }
                    else
                    {
                        object str = row[col.ColumnName].ToString();
                        excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str,
                                         ref cellformat);
                    }
                }
            }
            int ret = excel.CloseFile();
            return filename;
        }
        #region  清理过时的Excel文件

        private void ClearFile(string FilePath)
        {
            String[] Files = Directory.GetFiles(FilePath);
            if (Files.Length > 10)
            {
                for (int i = 0; i < 10; i++)
                {
                    try
                    {
                        File.Delete(Files[i]);
                    }
                    catch
                    {
                    }
                }
            }
        }

        #endregion

        public static bool CheckCard(string idNumber, int? memTypeID)
        {
            DataTable dt = SDDbSession.Default.FromSql("select mc.MemTypeID from [MemberInfo] as mi left join [MemCard] as mc on mi.MemCardID=mc.MemCardID where mi.IDNumber = '" + idNumber + "'").ToDataTable();
            int s = 0;
            if (dt != null&&dt.Rows.Count >0)
            {
                int.TryParse(dt.Rows[0]["MemTypeID"].ToString(),out s);
            }
            if (memTypeID.Equals(null))
            {
                return s.Equals(0);
            }
            return s.Equals(memTypeID);
        }

    }
}